<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>richstorage.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_storage_richstorage.js.html">richstorage.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2011 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Provides a convenient API for data with attached metadata
<a name="line17"></a> * persistence. You probably don&#39;t want to use this class directly as it
<a name="line18"></a> * does not save any metadata by itself. It only provides the necessary
<a name="line19"></a> * infrastructure for subclasses that need to save metadata along with
<a name="line20"></a> * values stored.
<a name="line21"></a> *
<a name="line22"></a> */
<a name="line23"></a>
<a name="line24"></a>goog.provide(&#39;goog.storage.RichStorage&#39;);
<a name="line25"></a>goog.provide(&#39;goog.storage.RichStorage.Wrapper&#39;);
<a name="line26"></a>
<a name="line27"></a>goog.require(&#39;goog.storage.ErrorCode&#39;);
<a name="line28"></a>goog.require(&#39;goog.storage.Storage&#39;);
<a name="line29"></a>goog.require(&#39;goog.storage.mechanism.Mechanism&#39;);
<a name="line30"></a>
<a name="line31"></a>
<a name="line32"></a>
<a name="line33"></a>/**
<a name="line34"></a> * Provides a storage for data with attached metadata.
<a name="line35"></a> *
<a name="line36"></a> * @param {!goog.storage.mechanism.Mechanism} mechanism The underlying
<a name="line37"></a> *     storage mechanism.
<a name="line38"></a> * @constructor
<a name="line39"></a> * @extends {goog.storage.Storage}
<a name="line40"></a> */
<a name="line41"></a>goog.storage.RichStorage = function(mechanism) {
<a name="line42"></a>  goog.base(this, mechanism);
<a name="line43"></a>};
<a name="line44"></a>goog.inherits(goog.storage.RichStorage, goog.storage.Storage);
<a name="line45"></a>
<a name="line46"></a>
<a name="line47"></a>/**
<a name="line48"></a> * Metadata key under which the actual data is stored.
<a name="line49"></a> *
<a name="line50"></a> * @type {string}
<a name="line51"></a> * @protected
<a name="line52"></a> */
<a name="line53"></a>goog.storage.RichStorage.DATA_KEY = &#39;data&#39;;
<a name="line54"></a>
<a name="line55"></a>
<a name="line56"></a>
<a name="line57"></a>/**
<a name="line58"></a> * Wraps a value so metadata can be associated with it. You probably want
<a name="line59"></a> * to use goog.storage.RichStorage.Wrapper.wrapIfNecessary to avoid multiple
<a name="line60"></a> * embeddings.
<a name="line61"></a> *
<a name="line62"></a> * @param {*} value The value to wrap.
<a name="line63"></a> * @constructor
<a name="line64"></a> */
<a name="line65"></a>goog.storage.RichStorage.Wrapper = function(value) {
<a name="line66"></a>  this[goog.storage.RichStorage.DATA_KEY] = value;
<a name="line67"></a>};
<a name="line68"></a>
<a name="line69"></a>
<a name="line70"></a>/**
<a name="line71"></a> * Convenience method for wrapping a value so metadata can be associated with
<a name="line72"></a> * it. No-op if the value is already wrapped or is undefined.
<a name="line73"></a> *
<a name="line74"></a> * @param {*} value The value to wrap.
<a name="line75"></a> * @return {(!goog.storage.RichStorage.Wrapper|undefined)} The wrapper.
<a name="line76"></a> */
<a name="line77"></a>goog.storage.RichStorage.Wrapper.wrapIfNecessary = function(value) {
<a name="line78"></a>  if (!goog.isDef(value) || value instanceof goog.storage.RichStorage.Wrapper) {
<a name="line79"></a>    return /** @type {(!goog.storage.RichStorage.Wrapper|undefined)} */ (value);
<a name="line80"></a>  }
<a name="line81"></a>  return new goog.storage.RichStorage.Wrapper(value);
<a name="line82"></a>};
<a name="line83"></a>
<a name="line84"></a>
<a name="line85"></a>/**
<a name="line86"></a> * Unwraps a value, any metadata is discarded (not returned). You might want to
<a name="line87"></a> * use goog.storage.RichStorage.Wrapper.unwrapIfPossible to handle cases where
<a name="line88"></a> * the wrapper is missing.
<a name="line89"></a> *
<a name="line90"></a> * @param {!Object} wrapper The wrapper.
<a name="line91"></a> * @return {*} The wrapped value.
<a name="line92"></a> */
<a name="line93"></a>goog.storage.RichStorage.Wrapper.unwrap = function(wrapper) {
<a name="line94"></a>  var value = wrapper[goog.storage.RichStorage.DATA_KEY];
<a name="line95"></a>  if (!goog.isDef(value)) {
<a name="line96"></a>    throw goog.storage.ErrorCode.INVALID_VALUE;
<a name="line97"></a>  }
<a name="line98"></a>  return value;
<a name="line99"></a>};
<a name="line100"></a>
<a name="line101"></a>
<a name="line102"></a>/**
<a name="line103"></a> * Convenience method for unwrapping a value. Returns undefined if the
<a name="line104"></a> * wrapper is missing.
<a name="line105"></a> *
<a name="line106"></a> * @param {(!Object|undefined)} wrapper The wrapper.
<a name="line107"></a> * @return {*} The wrapped value or undefined.
<a name="line108"></a> */
<a name="line109"></a>goog.storage.RichStorage.Wrapper.unwrapIfPossible = function(wrapper) {
<a name="line110"></a>  if (!wrapper) {
<a name="line111"></a>    return undefined;
<a name="line112"></a>  }
<a name="line113"></a>  return goog.storage.RichStorage.Wrapper.unwrap(wrapper);
<a name="line114"></a>};
<a name="line115"></a>
<a name="line116"></a>
<a name="line117"></a>/** @override */
<a name="line118"></a>goog.storage.RichStorage.prototype.set = function(key, value) {
<a name="line119"></a>  goog.base(this, &#39;set&#39;, key,
<a name="line120"></a>      goog.storage.RichStorage.Wrapper.wrapIfNecessary(value));
<a name="line121"></a>};
<a name="line122"></a>
<a name="line123"></a>
<a name="line124"></a>/**
<a name="line125"></a> * Get an item wrapper (the item and its metadata) from the storage.
<a name="line126"></a> *
<a name="line127"></a> * WARNING: This returns an Object, which once used to be
<a name="line128"></a> * goog.storage.RichStorage.Wrapper. This is due to the fact
<a name="line129"></a> * that deserialized objects lose type information and it
<a name="line130"></a> * is hard to do proper typecasting in JavaScript. Be sure
<a name="line131"></a> * you know what you are doing when using the returned value.
<a name="line132"></a> *
<a name="line133"></a> * @param {string} key The key to get.
<a name="line134"></a> * @return {(!Object|undefined)} The wrapper, or undefined if not found.
<a name="line135"></a> */
<a name="line136"></a>goog.storage.RichStorage.prototype.getWrapper = function(key) {
<a name="line137"></a>  var wrapper = goog.storage.RichStorage.superClass_.get.call(this, key);
<a name="line138"></a>  if (!goog.isDef(wrapper) || wrapper instanceof Object) {
<a name="line139"></a>    return /** @type {(!Object|undefined)} */ (wrapper);
<a name="line140"></a>  }
<a name="line141"></a>  throw goog.storage.ErrorCode.INVALID_VALUE;
<a name="line142"></a>};
<a name="line143"></a>
<a name="line144"></a>
<a name="line145"></a>/** @override */
<a name="line146"></a>goog.storage.RichStorage.prototype.get = function(key) {
<a name="line147"></a>  return goog.storage.RichStorage.Wrapper.unwrapIfPossible(
<a name="line148"></a>      this.getWrapper(key));
<a name="line149"></a>};
</pre>


</body>
</html>
